package cn.dnui_zzh807.dnuifood_16110100807.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.SomethingElse.InsertComments;
import cn.dnui_zzh807.dnuifood_16110100807.bean.CommentInfo;

public class PurchaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<CommentInfo> mDataList;
    private LayoutInflater mLayoutInflater;
    public PurchaseAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.purchase_adapter,parent,false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final CommentInfo entity = (CommentInfo) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.foodname.setText(entity.getFoodname());
        viewHolder.foodprice.setText(entity.getPrice());
        viewHolder.useraddress.setText(entity.getAddress());
        viewHolder.ordertime.setText(entity.getOrder_time());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String food_id = String.valueOf(mDataList.get(position).getItem_id());
                Intent intent = new Intent(mContext,InsertComments.class);
                intent.putExtra("food_id",food_id);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mDataList==null)
            return 0;
        else
            return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView foodname,foodprice,useraddress,ordertime;
        public ViewHolder(View view) {
            super(view);
             foodname=view.findViewById(R.id.purchase_name);
            foodprice=view.findViewById(R.id.purchase_price);
            useraddress=view.findViewById(R.id.purchase_address);
            ordertime=view.findViewById(R.id.purchase_time);
        }
    }
}
